package com.eascs.web.o2o.web.service;

import com.eascs.app.dao.JdbcSession;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.common.util.StringUtil;
import com.eascs.web.o2o.web.entity.BankLoanProgressEntity;
import com.eascs.web.o2o.web.form.BankLoanProgressForm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.List;

@Service
public class BankLoanProgressService {
	protected final Logger logger = LoggerFactory.getLogger(getClass());

	@Inject
	JdbcSession jdbcSession;

	//@Inject
	//JdbcSession jdbcSessionO2OService;
	
	@Inject
	JdbcSession jdbcSessionBiods;

	public boolean add(BankLoanProgressEntity entity) {
		return jdbcSessionBiods.insert(entity) > 0;
	}

	/**
	 * 更新记录
	 *
	 * @param entity 实体
	 * @return
	 */
	public boolean update(BankLoanProgressEntity entity) {
		String sql = "UPDATE bank_loan_progress_follow SET declare_status=?,  loan_amount=?" +
				" WHERE province=? AND platform_code=? AND loanee=? AND allocated_time=?";
		return jdbcSession.execute(sql, entity.getDeclareStatus(), entity.getLoanAmount(), entity.getProvince(),
				entity.getPlatformCode(), entity.getLoanee(), entity.getAllocatedTime()) > 0;
	}

	/**
	 * 根据四个条件去查询该记录是否存在
	 *
	 * @param province      省区
	 * @param plateformCode 平台编码
	 * @param loanee        贷款人
	 * @param allocatedTime 分配时间
	 * @return 是否
	 */
	public boolean findBankLoanProgressByParams(String province, String plateformCode, String loanee, String allocatedTime) {
		String sql = "SELECT COUNT(1) FROM ods_o2odev_bank_loan_progress_follow " +
				" WHERE province=? AND platform_code=? AND loanee=? AND allocated_time=?";
		return jdbcSessionBiods.findInteger(sql, province, plateformCode, loanee, allocatedTime) > 0;
	}

	public PageResult<Object[]> pageBankLoanProgress(BankLoanProgressForm form) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, form.getPageSize(), form.getShowPage());

		StringBuffer querySql = new StringBuffer("SELECT t.province, t.platform_code, t.loanee," +
				" t.loan_amount, t.allocated_time, t.submit_report_time" +
				", t.declare_status, t.remark, t.follower FROM ods_o2odev_bank_loan_progress_follow t WHERE 1=1 ");
		if (StringUtil.isNotBlank(form.getAllocatedTimeStart())) {
			querySql.append(" AND allocated_time>=?");
			sqlPageQuery.addParam(form.getAllocatedTimeStart());
		}
		if (StringUtil.isNotBlank(form.getAllocatedTimeEnd())) {
			querySql.append(" AND allocated_time<=?");
			sqlPageQuery.addParam(form.getAllocatedTimeEnd());
		}
		if (StringUtil.isNotBlank(form.getDeclareStatus())) {
			querySql.append(" AND declare_status=?");
			sqlPageQuery.addParam(form.getDeclareStatus());
		}
		sqlPageQuery.setQuerySql(querySql.toString());
		return jdbcSessionBiods.findDataWithPage(sqlPageQuery);
	}

	public List<Object[]> listBankLoanProgress(BankLoanProgressForm form) {
		StringBuffer querySql = new StringBuffer("SELECT t.province, t.platform_code, t.loanee," +
				" t.loan_amount, t.allocated_time, t.submit_report_time" +
				", t.declare_status, t.remark, t.follower FROM ods_o2odev_bank_loan_progress_follow t WHERE 1=1 " +
				" /**DATA_PERMISSION(t.platform_code)**/ ");
		if (StringUtil.isNotBlank(form.getAllocatedTimeStart())) {
			querySql.append(" AND allocated_time>='" + form.getAllocatedTimeStart() +"'");
		}
		if (StringUtil.isNotBlank(form.getAllocatedTimeEnd())) {
			querySql.append(" AND allocated_time<='" + form.getAllocatedTimeEnd() +"'");
		}
		if (StringUtil.isNotBlank(form.getDeclareStatus())) {
			querySql.append(" AND declare_status='" + form.getDeclareStatus() + "'");
		}
		return jdbcSessionBiods.findObjectsList(querySql.toString());
	}

	/**
	 * 从service数据库中校验该申请进度是否有效
	 *
	 * @param province      省区
	 * @param plateformCode 平台编码
	 * @param loanee        贷款人
	 * @return 是否
	 */
	public boolean checkLoaneeFromService(String province, String plateformCode, String loanee) {
		String sql = "SELECT COUNT(1) FROM ods_o2oservice_loanapply l LEFT JOIN ods_o2oservice_platform_regional r ON l.ltd = r.platform_id" +
				" LEFT JOIN ods_o2oservice_loan_user u ON u.loan_id=l.loan_id" +
				" WHERE r.province_name=? AND r.platform_code=? AND u.user_full_name=?";
		return jdbcSessionBiods.findInteger(sql, province, plateformCode, loanee) > 0;
	}

}
